#include "level2.h"

    



    int lvl2(int maxW, int maxH , GameObject *shuriken, GameObject * Ninja, int collFire, int mort)
   {
        SDL_Rect pos_ennemy;
        SDL_Rect pos_ennemy_mort;

        

        pos_ennemy.x = maxW-350;
        pos_ennemy.y = maxH-242;

        pos_ennemy_mort.x = maxW-350; 
        pos_ennemy_mort.y = maxH-200;



        Ninja->x = pos_ennemy.x;
        Ninja->y = pos_ennemy.y;
        Ninja->w = 160;
        Ninja->h = 207;
        if (mort ==0)
        {
            if(pos_ennemy.x - shuriken->x < 500)
            {
                 SDL_BlitSurface(Ninja->monstre1,NULL, jeu.ecran, &pos_ennemy);
            }
            else if (pos_ennemy.x - shuriken->x > 500)
            {
                SDL_BlitSurface(Ninja->monstre2,NULL, jeu.ecran, &pos_ennemy);
            }
            if (collFire == 1)
            {
                SDL_BlitSurface(Ninja->monstre3,NULL, jeu.ecran, &pos_ennemy);
            }
        }
        
        if (mort == 1)
        {
            SDL_BlitSurface(Ninja->monstre4,NULL, jeu.ecran, &pos_ennemy_mort);
        }


        


    

   }

   void attack(void)
{
    
  
        feu.sprite = IMG_Load("images/feu.png");


       
        feu.frameNumber = 0;
        feu.frameTimer = 1;

        
        feu.x = player.x+player.w;
        feu.y =player.y+50;
        

        
        feu.w = 45;
        feu.h = 30;
        
        

        

}
void attack2(void)
{
    
  
        feuG.sprite = IMG_Load("images/feuG.png");


       
        feuG.frameNumber = 0;
        feuG.frameTimer = 1;

        
        feuG.x = player.x+player.w;
        feuG.y =player.y+50;
        

        
        feuG.w = 45;
        feuG.h = 30;
        
        

        

}





int attackAnimRight(GameObject *entity, Hero *player, int maxW, int maxH, int collFire,GameObject *Ninja)
  {
    static int i=0,j=0,masque=0;

    if (i==0)
    {
        attack();
        playSoundFx(FEU);
        i=1;
    }
    if (entity->frameTimer <= 0)
    {
        
        entity->frameTimer = 1;

      
        entity->frameNumber++;

        
        if(entity->frameNumber >= entity->sprite->w / entity->w)
            entity->frameNumber = 0;

    }
   
    else
        entity->frameTimer--;


    

    
    SDL_Rect dest;
    SDL_Rect src;

   



    
    entity->x +=20;
    dest.x = entity->x+20;
    dest.y = entity->y+20;
    dest.w = entity->w;
    dest.h = entity->h;

  
    

   
    src.x = entity->frameNumber * entity->w;
    src.y = 0;
    src.w = entity->w;
    src.h = entity->h;

    if(dest.x > maxW)
    {
        i=0;
        j=0;

    }
    if (dest.x - player->x <= 300 && input.right == 0 && input.left == 0)
    {
   
     player->personnage[0] = IMG_Load("lvl2/attackright.png");
     player->personnage[1] = IMG_Load("lvl2/attackright.png");
     player->personnage[2] = IMG_Load("lvl2/attackright.png");
     player->personnage[3] = IMG_Load("lvl2/attackright.png");
     player->personnage[4] = IMG_Load("lvl2/attackright.png");
    }
    else
    {
        if ( input.right == 0 && input.left == 0)
        {
            player->personnage[0] = IMG_Load("perso/habib0.png");
            player->personnage[1] = IMG_Load("perso/habib0.png");
            player->personnage[2] = IMG_Load("perso/habib0.png");
            player->personnage[3] = IMG_Load("perso/habib0.png");
            player->personnage[4] = IMG_Load("perso/habib0.png");
            j=1;
        }
        if(input.jump == 1)
        {
            player->personnage[0] = IMG_Load("perso/saute1.png");
            player->personnage[1] = IMG_Load("perso/saute1.png");
            player->personnage[2] = IMG_Load("perso/saute1.png");
            player->personnage[3] = IMG_Load("perso/saute1.png");
            player->personnage[4] = IMG_Load("perso/saute1.png");
        }
    
 
}
if(collFire==1)
   {
    masque=1;
   }
   if (masque==1 && entity->x > Ninja->x)
   {
    masque =1;
   }
   else 
    masque=0;
    if(masque==0)
    {
        SDL_BlitSurface(entity->sprite, &src, jeu.ecran, &dest);
    }
    
    return i;


}



int attackAnimLeft(GameObject *entity, Hero *player, int maxW, int maxH)
  {
    static int i=0,j=0;

    if (i==0)
    {
        attack2();
        playSoundFx(FEU);
        i=1;
    }
    if (entity->frameTimer <= 0)
    {
        
        entity->frameTimer = 1;

      
        entity->frameNumber++;

        
        if(entity->frameNumber >= entity->sprite->w / entity->w)
            entity->frameNumber = 0;

    }
   
    else
        entity->frameTimer--;


    

    
    SDL_Rect dest;
    SDL_Rect src;

   



    
    entity->x -=20;
    dest.x = entity->x-20;
    dest.y = entity->y+20;
    dest.w = entity->w;
    dest.h = entity->h;

  
    

   
    src.x = entity->frameNumber * entity->w;
    src.y = 0;
    src.w = entity->w;
    src.h = entity->h;

   if(dest.x < 0)
    {
        i=0;
        j=0;

    }
    
    if (dest.x - player->x >= 0 && input.right == 0 && input.left == 0)
    {
       
     player->personnage[0] = IMG_Load("lvl2/attackleft.png");
     player->personnage[1] = IMG_Load("lvl2/attackleft.png");
     player->personnage[2] = IMG_Load("lvl2/attackleft.png");
     player->personnage[3] = IMG_Load("lvl2/attackleft.png");
     player->personnage[4] = IMG_Load("lvl2/attackleft.png");
    }
    else
    {
        if (input.right == 0 && input.left == 0)
        {
            player->personnage[0] = IMG_Load("perso/habibLeft0.png");
            player->personnage[1] = IMG_Load("perso/habibLeft0.png");
            player->personnage[2] = IMG_Load("perso/habibLeft0.png");
            player->personnage[3] = IMG_Load("perso/habibLeft0.png");
            player->personnage[4] = IMG_Load("perso/habibLeft0.png");
            j=1;
        }
        if(input.jump == 1)
        {
            player->personnage[0] = IMG_Load("perso/saute2.png");
            player->personnage[1] = IMG_Load("perso/saute2.png");
            player->personnage[2] = IMG_Load("perso/saute2.png");
            player->personnage[3] = IMG_Load("perso/saute2.png");
            player->personnage[4] = IMG_Load("perso/saute2.png");
        }
    
    }

    SDL_BlitSurface(entity->sprite, &src, jeu.ecran, &dest);
    return i;


}

void initshuriken(int maxW, int maxH)
{

    shuriken.sprite=IMG_Load("images/shuriken.png");

        shuriken.frameNumber = 0;
        shuriken.frameTimer = 1;

        
        shuriken.x = maxW-340;
        shuriken.y =maxH -220;

        
        shuriken.w = 55;
        shuriken.h = 46;
       
}
void initAura(int maxW, int maxH)
{

    aura.sprite=IMG_Load("lvl2/aura.png");

        aura.frameNumber = 0;
        aura.frameTimer = 3;

        
        aura.x = maxW-440;
        aura.y =maxH -330;

        
        aura.w = 316;
        aura.h = 279;
       
}


void animAura(GameObject *aura, int maxW, int maxH)
{
    static int i=0;

    if (i==0)
    {
        initAura(maxW, maxH);
        i=1;
    }

    if (aura->frameTimer <= 0)
    {
        
        aura->frameTimer = 3;

      
        aura->frameNumber++;

        
        if(aura->frameNumber >= aura->sprite->w / aura->w)
            aura->frameNumber = 0;

    }
   
    else
        aura->frameTimer--;


    

    
    SDL_Rect dest;

    
    shuriken.x -= 30;
    dest.x = aura->x+20;
    dest.y = aura->y+20;
    dest.w = aura->w;
    dest.h = aura->h;

  
    SDL_Rect src;

   
    src.x = aura->frameNumber * aura->w;
    src.y = 0;
    src.w = aura->w;
    src.h = aura->h;

    SDL_BlitSurface(aura->sprite, &src, jeu.ecran, &dest);
}



void attackAnimShuriken(GameObject *entity, int coll, Hero *player, int maxW, int maxH)
  {
    static int i=0,j=0, masque=0;

    if (i==0)
    {
        initshuriken(maxW, maxH);
        playSoundFx(SHURIKEN);
        i=1;
    }
    
    if (entity->frameTimer <= 0)
    {
        
        entity->frameTimer = 1;

      
        entity->frameNumber++;

        
        if(entity->frameNumber >= entity->sprite->w / entity->w)
            entity->frameNumber = 0;

    }
   
    else
        entity->frameTimer--;


    

    
    SDL_Rect dest;

    
    shuriken.x -= 30;
    dest.x = entity->x+20;
    dest.y = entity->y+20;
    dest.w = entity->w;
    dest.h = entity->h;

  
    SDL_Rect src;

   
    src.x = entity->frameNumber * entity->w;
    src.y = 0;
    src.w = entity->w;
    src.h = entity->h;

    if (shuriken.x < -2000)
    {
        i=0;
    }

   if(coll==1)
   {
    masque=1;
   }
   if (masque==1 && entity->x < player->x)
   {
    masque =1;
   }
   else 
    masque=0;
   
    if (masque==0)
    {
        SDL_BlitSurface(entity->sprite, &src, jeu.ecran, &dest);
    }
    

}

int collide2 (GameObject *shuri, Hero *player)
{
    
    if ((player->x >= shuri->x + shuri->w) || (player->x + player->w <= shuri->x) || (player->y >= shuri->y + shuri->h) || (player->y + player->h <= shuri->y) )
      {
        
        return 0;
      } 
       
    
    else if (player->y + player->h <= shuri->y + 10)
    {
        
        
        return 2;
        
    }
    
    else
    {

        playSoundFx(HIT);
        
        return 1;
        
    }
        
}
int collideFire (GameObject *feu, GameObject *Ninja)
{
  
    
    if ((Ninja->x >= feu->x + feu->w) || (Ninja->x + Ninja->w <= feu->x) || (Ninja->y >= feu->y + feu->h) || (Ninja->y + Ninja->h <= feu->y) )
      {
        
        return 0;
      } 
       
    
    else if (Ninja->y + Ninja->h <= feu->y + 10)
    {
        
        
        return 2;
        
    }
    
    else
    {

        
        
        return 1;
        
    }
        
}

 void drawJauje(int maxW,int imgW, int imgWidthEnnemy)
    {
    
    char text[200];


    
    drawImage(jeu.jaugePlayer, 120, 8); 
    drawImage(jeu.jaugeEnnemy, maxW-300, -2); 
    drawLifeJauge(player.HP, 193, 30, imgW); 
    drawLifeJauge(player.HP, 193, 45,imgW); 
    drawLifeJauge(Ninja.HP, maxW-280, 30,imgWidthEnnemy); 
    drawLifeJauge(Ninja.HP, maxW-280, 45,imgWidthEnnemy); 
    sprintf(text, "%d X ", jeu.vies);
    drawString(text, 70, 30, font);

    
    
  
    }